package com.qfmy.webapp.controller.user;

import com.qfmy.common.result.Result;
import com.qfmy.common.utils.IpUtils;
import com.qfmy.model.annotation.LoginLog;
import com.qfmy.model.entity.userInfo.UserInfo;
import com.qfmy.model.query.user.UserQuery;
import com.qfmy.model.vo.user.LoginVo;
import com.qfmy.model.vo.user.UserInfoVo;
import com.qfmy.webapp.service.user.UserInfoService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 清风明月
 * @Package com.qfmy.webapp.controller.user
 * @date 2025/11/11
 * @description 用户信息类
 */
@RequestMapping("/app/userInfo")
@RestController
@SuppressWarnings("all")
public class UserInfoController {

    /**
     * 注入用户service
     */
    @Autowired
    private UserInfoService userInfoService;


    /**
     * 获取短信验证码
     */
    @GetMapping("/login/getSmsCode")
    public Result<String> getSmsCode(@RequestParam String phone) {
        userInfoService.getSmsCode(phone);
        return Result.ok("发送成功");
    }

    /**
     * 手机号 + 短信验证码登入
     */
    @LoginLog(operation = "手机号登入")
    @PostMapping("/login/loginPhoneAndCode")
    public Result<LoginVo> loginPhoneAndCode(@RequestBody UserQuery userQuery,HttpServletRequest request) {
        // 获取客户端IP地址
        String clientIp = IpUtils.getRealIp(request);
        userQuery.setIp(clientIp);
        LoginVo loginVo =  userInfoService.loginPhoneAndCode(userQuery);
        return Result.ok(loginVo);
    }

    /**
     * 获取临时登录令牌
     * @return
     */
    @RequestMapping("/login/getTempLoginToken")
    public Result<String> getTempLoginToken(){
        String token = userInfoService.getTempLoginToken();
        return Result.ok(token);
    }

    /**
     * 获取用户的二维码
     */
    @GetMapping("/getUserQrCode")
    public Result<String> getUserQrCode() {
        String qrCode = userInfoService.getUserQrCode();
        return Result.ok(qrCode);
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/updateInfo")
    public Result updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfoService.updateUserInfo(userInfo);
        return Result.ok();
    }

    /**
     * 进行注销账号
     */
    @GetMapping("/logout")
    public Result logout() {
        userInfoService.logout();
        return Result.ok();
    }

    /**
     * 退出登入
     */
    @GetMapping("/exit")
    public Result exit() {
        userInfoService.exit();
        return Result.ok();
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/getUserInfo")
    public Result<UserInfoVo> getUserInfo(){
        UserInfoVo userInfo = userInfoService.getUserInfo();
        return Result.ok(userInfo);
    }

    /**
     * 更新用户头像
     */
    @GetMapping("/updateUserAvatar")
    public Result updateUserAvatar(@RequestParam String avatar) {
        userInfoService.updateUserAvatar(avatar);
        return Result.ok("成功保存");
    }

    /**
     * 获取用户的阅读,听书,观看时长
     */
    @GetMapping("/getUserDuration")
    public Result<String> getUserDuration() {
        String duration = userInfoService.getUserDuration();
        return Result.ok(duration);
    }


    /**
     * 更新用户喜欢类型
     */
    @RequestMapping("/updateUserLikeType")
    public Result updateUserLikeType(@RequestParam String categoryName) {
        userInfoService.updateUserLikeType(categoryName);
        return Result.ok();
    }

    /**
     * 更新用户的阅读时间
     */
    @GetMapping("/updateUserReadTime")
    public Result updateUserReadTime(@RequestParam Integer time) {
        userInfoService.updateUserReadTime(time);
        return Result.ok();
    }

    /**
     * 更新用户的支付密码
     */
    @GetMapping("/updateUserPayPassword")
    public Result updateUserPayPassword(@RequestParam String payPassword) {
        userInfoService.updateUserPayPassword(payPassword);
        return Result.ok();
    }

    /**
     * 更新用户今日收益
     */
    @GetMapping("/updateUserTodayIncome")
    public Result updateUserTodayIncome(@RequestParam Integer income) {
        userInfoService.updateUserTodayIncome(income);
        return Result.ok();
    }

    /**
     * 获取用户今日收益
     */
    @GetMapping("/getUserTodayIncome")
    public Result<Integer> getUserTodayIncome() {
        Integer income = userInfoService.getUserTodayIncome();
        return Result.ok(income);
    }


}
